<?php

namespace app\admin\controller\iot\statistics;

use app\admin\service\iot\statistics\Statistics;
use app\BaseController;
use app\common\Message;
use think\App;
use think\response\Json;

class StatisticsDate extends BaseController
{
    use Message;

    /**
     * @var array|mixed
     */
    private mixed $data;
    private Statistics $statisticsDates;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $data = $this->request->param();
        $data['uid'] = $this->userData['id'];
        $data['role_mode'] = $this->userData['role_mode'];
        //单位id
        $data['platform_unit_id'] = $this->userData['unit_id'];
        $this->statisticsDates = new Statistics();
        $this->data = $data;
    }

    /**
     * 统计设备数量
     * @return bool|string|Json
     */
    public function numberOfDevices(): bool|string|Json
    {
        try {
            if(!$this->statisticsDates->numberOfDevices($this->data)) throw new \Exception($this->statisticsDates->getMessage());
            return success($this->statisticsDates->getResult(),$this->statisticsDates->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 物联网数据概况
     * @return bool|string
     */
    public function iotOverview(): bool|string
    {
        try {
            if(!$this->statisticsDates->iotOverview($this->data)) throw new \Exception($this->statisticsDates->getMessage());
            return success($this->statisticsDates->getResult(),$this->statisticsDates->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 功能模块监测统计
     * @return bool|string
     */
    public function moduleMonitoring(): bool|string
    {
        try {
            if(!$this->statisticsDates->moduleMonitoring($this->data)) throw new \Exception($this->statisticsDates->getMessage());
            return success($this->statisticsDates->getResult(),$this->statisticsDates->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 各设备用户占比统计
     * @return bool|string
     */
    public function userDeviceStatistic(): bool|string
    {
        try {
            if(!$this->statisticsDates->userDeviceStatistic($this->data)) throw new \Exception($this->statisticsDates->getMessage());
            return success($this->statisticsDates->getResult(),$this->statisticsDates->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 功能室排行
     * @return bool|string
     */
    public function Ranking(): bool|string
    {
        try {
            if(!$this->statisticsDates->Ranking($this->data)) throw new \Exception($this->statisticsDates->getMessage());
            return success($this->statisticsDates->getResult(),$this->statisticsDates->getMessage());
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}